import axios, {AxiosInstance, AxiosPromise, AxiosRequestConfig} from 'axios';
import qs from 'qs';
import store from '@/store/index';
import buildConfig from '@/util/CommonParam';

class AuthToken {
  public token!: string;
  public platform!: string;
  public browser!: string;
}

const req: AxiosInstance = axios.create({
  baseURL: buildConfig.useProxyUrl,
  timeout: 60000,
  paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'indices' }),
});

req.interceptors.request.use((config) => {
  config.headers[`${buildConfig.authTokenKeyName}`] = JSON.stringify(getAuth());
  return config;
}, (error) => {
  return error;
});

req.interceptors.response.use((config) => {
  // if (config.headers['content-type'] === 'application/json') {
  //   return config.data;
  // }
  return config;
}, ((error) => {
  return error;
}));


function getAuth(): AuthToken {
  const authToken = new AuthToken();
  authToken.token = store.getters['user/token'];
  return authToken;
}
export const request = req;
